package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.Notice;
import com.gaogzhen.service.NoticeService;
import com.gaogzhen.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Administrator
 * @version 1.0
 * @description 公告管理器
 * @date 2022-05-31 10:24
 */
@RestController
@RequestMapping("/notices")
@Api(tags = "公告管理")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getList")
    @ApiOperation("分页查询公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "title", value = "公告标题"),
            @ApiImplicitParam(name = "startTime", value = "公告开始时间"),
            @ApiImplicitParam(name = "endTime", value = "公告结束时间"),
            @ApiImplicitParam(name = "status", value = "启用禁用状态")
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> findByPage(@ApiIgnore Page<Notice> page, String title, String startTime, String endTime, Integer status) {
        return R.ok(noticeService.findByPage(page, title, startTime, endTime, status));
    }

    @GetMapping
    @ApiOperation("获取一个公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告id")
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Notice> add(Long id) {
       return R.ok(noticeService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "公告信息")
    })
    @PreAuthorize("hasAuthority('notice_create')")
    public R add(@RequestBody @Validated Notice notice) {
        noticeService.add(notice);
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("更新公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "公告信息")
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R update(@RequestBody @Validated Notice notice) {
        noticeService.updateById(notice);
        return R.ok();
    }



    @DeleteMapping
    @ApiOperation("删除公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "公告id集合")
    })
    @PreAuthorize("hasAuthority('notice_delete')")
    public R<Page<Notice>> delete(@RequestBody String []ids) {
        noticeService.delete(ids);
        return R.ok();
    }

    @PatchMapping("/updateStatus")
    @ApiOperation("启用禁用公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告id"),
            @ApiImplicitParam(name = "status", value = "启用禁用状态"),
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R updateStatus(@RequestParam Long id, @RequestParam Integer status) {
        noticeService.updateStatus(id, status);
        return R.ok();
    }

    @GetMapping("/simple")
    @ApiOperation("查询前台展示的notice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "每页条目"),
    })
    public R<Page<Notice>> findNotices(@ApiIgnore Page<Notice> page) {
        return R.ok(noticeService.findNotices(page));
    }

    @GetMapping("/simple/{id}")
    @ApiOperation("前台展示的某个notice详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告id")
    })
    public R<Notice> noticeSimpleInfo(@PathVariable("id") Long id) {
        return R.ok(noticeService.getById(id));
    }
}
